package org.pb;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.OverlayItem;

public class LocationOnMap extends MapActivity implements OnClickListener {
	private MapView map = null;
	private MyLocationOverlay me = null;
	private Drawable drawable;
	private LocationManager mgr;
	private Location location;
	private GeoPoint geopoint;
	private GeoPoint pinPoint;

	public void onClick(View arg0) {

		switch (arg0.getId()) {
		case R.id.button1:
			Intent intent = new Intent();
	    try{
			intent.putExtra("latitude", pinPoint.getLatitudeE6());
			intent.putExtra("longitude", pinPoint.getLongitudeE6());
			
			setResult(RESULT_OK, intent);
			finish();
		}
		catch(Exception e){
	        Toast.makeText(this, "Najpierw wskaż pozycję na mapie", Toast.LENGTH_SHORT).show();
	        
		}
			break;
		}

	}
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.locationonmap);
        View getCoordinatesButton = findViewById(R.id.button1); 
        getCoordinatesButton.setOnClickListener(this);
		map = (MapView) findViewById(R.id.mapView);
		mgr = (LocationManager) getSystemService(LOCATION_SERVICE);
		Criteria criteria = new Criteria();
		location = mgr
				.getLastKnownLocation(mgr.getBestProvider(criteria, true));
		if (location != null) {
			geopoint = new GeoPoint((int) (location.getLatitude() * 1000000),
					(int) (location.getLongitude() * 1000000));
			map.getController().setCenter(geopoint);
		}

		map.getController().setZoom(17);
		map.setBuiltInZoomControls(true);

		Drawable marker = getResources().getDrawable(R.drawable.taxipin);

		marker.setBounds(0, 0, marker.getIntrinsicWidth(),
				marker.getIntrinsicHeight());

		map.getOverlays().add(new SitesOverlay(marker));

		me = new MyLocationOverlay(this, map);
		map.getOverlays().add(me);
	}

	@Override
	public void onResume() {
		super.onResume();

		me.enableCompass();
	}

	@Override
	public void onPause() {
		super.onPause();

		me.disableCompass();
	}

	@Override
	protected boolean isRouteDisplayed() {
		return (false);
	}

	private class SitesOverlay extends ItemizedOverlay<OverlayItem> {
		private List<OverlayItem> items = new ArrayList<OverlayItem>();
		private Drawable marker = null;

		public SitesOverlay(Drawable marker) {
			super(marker);
			this.marker = marker;
			populate();
		}

		@Override
		protected OverlayItem createItem(int i) {
			return (items.get(i));
		}

		@Override
		public void draw(Canvas canvas, MapView mapView, boolean shadow) {
			super.draw(canvas, mapView, shadow);

			boundCenterBottom(marker);
		}

		@Override
		public int size() {
			return (items.size());
		}

		@Override
		public boolean onTap(GeoPoint p, MapView mapView) {
			drawable = getResources().getDrawable(R.drawable.taxipin);
			SitesOverlay itemizedOverlay = new SitesOverlay(drawable);
			int lat = (int) p.getLatitudeE6();
			int lng = (int) p.getLongitudeE6();

			pinPoint = new GeoPoint(lat, lng);
			OverlayItem overlayitem = new OverlayItem(pinPoint, "", "");
			items.clear();
			items.add(overlayitem);
			populate();

			return true;
		}
	}
}